package es.duma.search;

import es.duma.service.flickr.SearchRESTflickr;
import es.duma.service.youtube.SearchRESTyoutube;
import es.duma.util.log.Log;

import es.duma.util.xsl.TransformXSL;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.ObjectOutputStream;
import java.io.OutputStream;

import java.io.PrintStream;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import java.util.ArrayList;
import java.util.Enumeration;

import java.util.Iterator;

import javax.ejb.Stateless;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

@Stateless(name="SearchREST")
public class SearchRESTBean implements SearchREST, SearchRESTLocal {
    public SearchRESTBean() {
    }

    public String search(InputStream xmlSearch) {
        Log.debug(this,"Llamada al metodo search");
        SearchObject searchOBJ=new SearchObject();
        
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser=null;
        try {
            saxParser = factory.newSAXParser();
            saxParser.parse(xmlSearch,new SearchHandler(searchOBJ));
        } catch (SAXException e) {
            // TODO
        } catch (ParserConfigurationException e) {
            // TODO
        } catch (IOException e) {
            // TODO
        }
        
        
        ArrayList urls_search_rest=new ArrayList();
        String url_search="";
        //En el objeto search tenemos encapsulada la busqueda enviada por XML
        //Obtener la direccion REST para el servicio youtube    
        SearchRESTService youtube=new SearchRESTyoutube();
        url_search=youtube.getREST_URL(searchOBJ);
        Log.debug(this,"URL de busqueda youtube: "+url_search);
        urls_search_rest.add(url_search);
      
        //Obtener la direccion REST para el servicio flickr
        SearchRESTService flickr=new SearchRESTflickr();
        url_search=flickr.getREST_URL(searchOBJ);
        Log.debug(this,"URL de busqueda flickr: "+url_search);
        //urls_search_rest.add(url_search);
        
        InputStream in=null;
        OutputStream out=new ByteArrayOutputStream();
        HttpURLConnection connection=null;
        Iterator it=urls_search_rest.iterator();
        while (it.hasNext()){
            URL url;
            String urlString=(String)it.next();
            try {
                url = new URL(urlString);
                connection = (HttpURLConnection)url.openConnection();
            }catch (MalformedURLException e) {
                Log.error(this,"Error al formar la URL "+e);
            }catch (IOException e) {
                Log.error(this,"No se puede conectar");
            }
            
            try {
                in = connection.getInputStream();
            } catch (IOException e) {
                Log.error(this,"Error en el buffer");
            }
            
            //Realizar transformacion del formato de metadatos propietario al universal
            String xslPath=youtube.getXslPath();
            TransformXSL xsl_transformer=new TransformXSL(xslPath);
            try {
                xsl_transformer.print(in,out);
            } catch (IOException e) {
                // TODO
            }
            connection.disconnect(); 
            
            
        }
        
        
        
        
        
        
        
        
        
        return out.toString();
    }
}
